package com.ruoyi.system.service.impl.video;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.model.VsUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.video.*;
import com.ruoyi.system.mapper.video.VsDramasVideoFavoriteMapper;
import com.ruoyi.system.mapper.video.VsDramasVideoMapper;
import com.ruoyi.system.mapper.video.VsDramasVideoOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.video.VsDramasVideoEpisodesMapper;
import com.ruoyi.system.service.video.IVsDramasVideoEpisodesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 剧集Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-19
 */
@Service
public class VsDramasVideoEpisodesServiceImpl implements IVsDramasVideoEpisodesService 
{
    @Autowired
    private VsDramasVideoEpisodesMapper vsDramasVideoEpisodesMapper;

    @Autowired
    private VsDramasVideoLogServiceImpl vsDramasVideoLogService;

    @Autowired
    private VsDramasVideoOrderMapper vsDramasVideoOrderMapper;

    @Autowired
    private VsDramasVideoFavoriteMapper vsDramasVideoFavoriteMapper;

    @Autowired
    private VsDramasVideoMapper vsDramasVideoMapper;

    /**
     * 查询剧集
     * 
     * @param id 剧集主键
     * @return 剧集
     */
    @Override
    public VsDramasVideoEpisodes selectVsDramasVideoEpisodesById(Integer id)
    {
        return vsDramasVideoEpisodesMapper.selectVsDramasVideoEpisodesById(id);
    }

    /**
     * 查询剧集列表
     * 
     * @param vsDramasVideoEpisodes 剧集
     * @return 剧集
     */
    @Override
    public List<VsDramasVideoEpisodes> selectVsDramasVideoEpisodesList(VsDramasVideoEpisodes vsDramasVideoEpisodes)
    {
        VsUser user = null;
        try {
            user = SecurityUtils.getLoginUser().getVsUser();
        } catch (Exception e) {
            System.out.println(e);
        }
        if (user != null) {
            vsDramasVideoEpisodes.setUserId(user.getId());
        }
        List<VsDramasVideoEpisodes> episodesList = vsDramasVideoEpisodesMapper.selectVsDramasVideoEpisodesList(vsDramasVideoEpisodes);
        List<VsDramasVideoLog> logList = new ArrayList<>();
        List<VsDramasVideoFavorite> likeList = new ArrayList<>();
        List<VsDramasVideoFavorite> favoriteList = new ArrayList<>();
        List<VsDramasVideoOrder> orderList = new ArrayList<>();
        if (user != null && vsDramasVideoEpisodes.getVid() != null) {
            // 先去查找用户的订单信息和观看记录
            VsDramasVideoLog videoLog = new VsDramasVideoLog();
            videoLog.setType("log");
            videoLog.setUserId(user.getId());
            videoLog.setVid(vsDramasVideoEpisodes.getVid());
            logList = vsDramasVideoLogService.selectVsDramasVideoLogList(videoLog);
            VsDramasVideoOrder videoOrder = new VsDramasVideoOrder();
            videoOrder.setUserId(user.getId());
            videoOrder.setVid(vsDramasVideoEpisodes.getVid());
//            videoOrder.setEpisodeId(Long.valueOf(vsDramasVideoEpisodes.getId()));
            orderList = vsDramasVideoOrderMapper.selectVsDramasVideoOrderList(videoOrder);
        }
        for (VsDramasVideoEpisodes v : episodesList) {
            int i = v.getLikes() + v.getFakeLikes();
            if (i > 1000) {
                String s = (i / 1000) + "k";
                v.setSumLikes(s);
            } else {
                v.setSumLikes(String.valueOf(i));
            }
            int g = v.getVsDramasVideo().getFavorites() + v.getVsDramasVideo().getFakeFavorites();
            if (g > 1000) {
                String s = (g / 1000) + "k";
                v.getVsDramasVideo().setSumfavorites(s);
            } else {
                v.getVsDramasVideo().setSumfavorites(String.valueOf(g));
            }
            if (user != null) {
                VsDramasVideoFavorite videoFavorite = new VsDramasVideoFavorite();
                videoFavorite.setType("favorite");
                videoFavorite.setUserId(user.getId());
                videoFavorite.setVid(vsDramasVideoEpisodes.getVid());
                favoriteList = vsDramasVideoFavoriteMapper.selectVsDramasVideoFavoriteList(videoFavorite);
                if (favoriteList.size() > 0) {
                    v.getVsDramasVideo().setFavorite(true);
                } else {
                    v.getVsDramasVideo().setFavorite(false);
                }
                VsDramasVideoFavorite videoLike = new VsDramasVideoFavorite();
                videoLike.setType("like");
                videoLike.setUserId(user.getId());
                videoLike.setVid(vsDramasVideoEpisodes.getVid());
                likeList = vsDramasVideoFavoriteMapper.selectVsDramasVideoFavoriteList(videoLike);
                if (likeList.size() > 0) {
                    for (VsDramasVideoFavorite f : likeList) {
                        if (f.getEpisodeId().equals((long) v.getId())) {
                            v.setLike(true);
                        } else {
                            v.setLike(false);
                        }
                    }
                } else {
                    v.setLike(false);
                }
                for (VsDramasVideoLog l : logList) {
                    if (l.getEpisodeId().equals((long) v.getId())) {
                        episodesList.get(0).getVsDramasVideo().setCurrentEpisodeId(Long.valueOf(v.getId()));
                        episodesList.get(0).getVsDramasVideo().setCurrentViewTime(l.getViewTime());
                        episodesList.get(0).getVsDramasVideo().setCurrentName(v.getName());
                    }
                }
                // 如果视频不免费就接着往下走
                if (v.getPrice() != 0) {
                    // 判断不免费的视频用户是否有购买记录
                    // 如果不免费并且也不是vip免费就需要购买
                    if (orderList.size() == 0) {
                        if (v.getPrice() != 0) {
                            if (v.getVprice() != 0 || (user.getVipExpiretime() == null || user.getVipExpiretime() < System.currentTimeMillis())) {
                                v.setVideo("");
                            }
                        }
                    } else {
                        int num = 0;
                        // 现在是只判断付费的剧集信息
                        for (VsDramasVideoOrder log : orderList) {
                            // 首先判断是否有订单记录，没有订单记录的视频先看是否免费
                            // 如果视频免费就让用户看，如果不免费查看视频是否是vip免费并且用户是否是vip
                            // 如果用户是vip并且视频是vip免费看的就返回视频链接，否则就需要购买
                            if (log.getEpisodeId().equals((long) v.getId())) {
                                // 如果num大于1就代表这集有付费记录，可以让用户观看
                                if (num > 0) {
                                    break;
                                }
                                num++;
                            }
                        }
                        if (num == 0) {
                            // 如果这集没有订单记录就看这集是否免费
                            // 如果不免费就看是否是vip免费
                            // 如果vip免费并且vip没有过期就允许观看，否则不让看
                            if (v.getPrice() != 0) {
                                if (v.getVprice() != 0) {
                                    v.setVideo("");
                                } else {
                                    if (user.getVipExpiretime() == null) {
                                        v.setVideo("");
                                    } else {
                                        if (user.getVipExpiretime() < System.currentTimeMillis()) {
                                            v.setVideo("");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                // 如果获取不到当前用户信息就只返回免费的剧集链接
                if (v.getPrice() != 0) {
                    v.setVideo("");
                }
            }
        }
        return episodesList;
    }

    /**
     * 新增剧集
     * 
     * @param vsDramasVideoEpisodes 剧集
     * @return 结果
     */
    @Override
    public int insertVsDramasVideoEpisodes(VsDramasVideoEpisodes vsDramasVideoEpisodes)
    {
        vsDramasVideoEpisodes.setCreatetime(System.currentTimeMillis());
        vsDramasVideoEpisodes.setUpdatetime(System.currentTimeMillis());
        return vsDramasVideoEpisodesMapper.insertVsDramasVideoEpisodes(vsDramasVideoEpisodes);
    }

    /**
     * 修改剧集
     * 
     * @param vsDramasVideoEpisodes 剧集
     * @return 结果
     */
    @Override
    public int updateVsDramasVideoEpisodes(VsDramasVideoEpisodes vsDramasVideoEpisodes)
    {
        vsDramasVideoEpisodes.setUpdatetime(System.currentTimeMillis());
        return vsDramasVideoEpisodesMapper.updateVsDramasVideoEpisodes(vsDramasVideoEpisodes);
    }

    /**
     * 批量删除剧集
     * 
     * @param ids 需要删除的剧集主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasVideoEpisodesByIds(Integer[] ids)
    {
        return vsDramasVideoEpisodesMapper.deleteVsDramasVideoEpisodesByIds(ids);
    }

    /**
     * 删除剧集信息
     * 
     * @param id 剧集主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasVideoEpisodesById(Integer id)
    {
        return vsDramasVideoEpisodesMapper.deleteVsDramasVideoEpisodesById(id);
    }

    @Override
    public List<VsDramasVideoEpisodes> recommendList(VsDramasVideoEpisodes vsDramasVideoEpisodes) {
        List<VsDramasVideoEpisodes> list = vsDramasVideoEpisodesMapper.recommendList(vsDramasVideoEpisodes);
        VsUser user = null;
        try {
            user = SecurityUtils.getLoginUser().getVsUser();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        List<VsDramasVideoLog> logList = new ArrayList<>();
        if (user != null) {
            VsDramasVideoLog videoLog = new VsDramasVideoLog();
            videoLog.setType("log");
            videoLog.setUserId(user.getId());
            videoLog.setVid(vsDramasVideoEpisodes.getVid());
            logList = vsDramasVideoLogService.selectVsDramasVideoLogList(videoLog);
        }
        for (VsDramasVideoEpisodes e : list) {
            int i = e.getLikes() + e.getFakeLikes();
            if (i > 1000) {
                String s = (i / 1000) + "k";
                e.setSumLikes(s);
            } else {
                e.setSumLikes(String.valueOf(i));
            }
            List<VsDramasVideoFavorite> favoriteList = new ArrayList<>();
            List<VsDramasVideoFavorite> likeList = new ArrayList<>();
            if (user != null) {
                VsDramasVideoFavorite videoFavorite = new VsDramasVideoFavorite();
                videoFavorite.setType("favorite");
                videoFavorite.setUserId(user.getId());
                videoFavorite.setVid(vsDramasVideoEpisodes.getVid());
                favoriteList = vsDramasVideoFavoriteMapper.selectVsDramasVideoFavoriteList(videoFavorite);
                if (favoriteList.size() > 0) {
                    e.getVsDramasVideo().setFavorite(true);
                } else {
                    e.getVsDramasVideo().setFavorite(false);
                }
                VsDramasVideoFavorite videoLike = new VsDramasVideoFavorite();
                videoLike.setType("like");
                videoLike.setUserId(user.getId());
                videoLike.setVid(vsDramasVideoEpisodes.getVid());
                likeList = vsDramasVideoFavoriteMapper.selectVsDramasVideoFavoriteList(videoLike);
                if (likeList.size() > 0) {
                    for (VsDramasVideoFavorite f : likeList) {
                        if (f.getEpisodeId().equals((long) e.getId())) {
                            e.setLike(true);
                        } else {
                            e.setLike(false);
                        }
                    }
                } else {
                    e.setLike(false);
                }
                for (VsDramasVideoLog l : logList) {
                    if (l.getEpisodeId().equals((long) e.getId())) {
                        list.get(0).getVsDramasVideo().setCurrentEpisodeId(Long.valueOf(e.getId()));
                        list.get(0).getVsDramasVideo().setCurrentViewTime(l.getViewTime());
                        list.get(0).getVsDramasVideo().setCurrentName(e.getName());
                    }
                }
            }
        }
        return list;
    }

    @Transactional
    @Override
    public int increaseEpisodes(VsDramasVideoEpisodes vsDramasVideoEpisodes) {
        VsDramasVideoEpisodes episodes = new VsDramasVideoEpisodes();
        episodes.setId(vsDramasVideoEpisodes.getId());
        episodes.setUpdatetime(System.currentTimeMillis());
        int e = vsDramasVideoEpisodesMapper.increaseEpisodes(episodes);
        VsDramasVideo video = new VsDramasVideo();
        video.setId(Math.toIntExact(vsDramasVideoEpisodes.getVid()));
        video.setUpdatetime(System.currentTimeMillis());
        int v = vsDramasVideoMapper.increaseVideo(video);
        if ((e+v) > 1) {
            return 1;
        }
        return 0;
    }

    @Override
    public List<VsDramasVideoEpisodes> getList(VsDramasVideoEpisodes vsDramasVideoEpisodes) {
        return vsDramasVideoEpisodesMapper.getList(vsDramasVideoEpisodes);
    }

    @Override
    public int batchInsertEpisodes(List<VsDramasVideoEpisodes> list) {
        long currentTimeMillis = System.currentTimeMillis();
        String defaultStatus = "normal";
        //循环插入数据
        for (VsDramasVideoEpisodes e : list) {
            e.setSales(0);
            e.setLikes(0);
            e.setViews(0);
            e.setFavorites(0);
            e.setShares(0);
            e.setStatus(defaultStatus);
            e.setCreatetime(currentTimeMillis);
            e.setUpdatetime(currentTimeMillis);
        }
        return vsDramasVideoEpisodesMapper.batchInsertEpisodes(list);
    }
}
